package com.gzx.bi.modular.project.service.impl;

import com.alibaba.fastjson.JSON;
import com.baomidou.dynamic.datasource.annotation.DS;
import com.gzx.bi.modular.project.mapper.GzxProjectDebtMapper;
import com.gzx.bi.modular.project.service.GzxProjectDebtService;
import com.gzx.bi.modular.project.vo.GzxProjectDebtVo;
import com.gzx.bi.modular.user.entity.GzxUserDebt;
import com.gzx.bi.modular.user.mapper.GzxUserDebtMapper;
import com.gzx.bi.modular.user.vo.GzxUserDebtVo;
import com.gzx.tjzy.feign.GzxProjectFeign;
import com.gzx.tjzy.feign.resp.project.GzxProjectRespDto;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import vip.xiaonuo.sys.feign.SysUserFeign;

import java.util.List;

/**
 * 项目的在催案件和在催人力
 */
@Slf4j
@Service
@DS("bi")
@RequiredArgsConstructor
public class GzxProjectDebtServiceImpl implements GzxProjectDebtService {

    private final GzxProjectFeign projectFeign;

    private final GzxProjectDebtMapper debtMapper;

    private final SysUserFeign sysUserFeign;

    private final GzxUserDebtMapper userDebtMapper;

    /**
     * 项目的在催案件和在催人力
     */
    @Override
    public void saveProjectDebt() {
        //同步项目的在催案件和在催人力
        syncProjectDebt();
        //同步在催人力的所属项目和所属小组
        syncUserProjectGroup();
    }

    /**
     * 同步在催人力所属项目所属小组
     */
    @DS("bi")
    private void syncUserProjectGroup() {
        String allUser = sysUserFeign.getAllUser();
        //把user列表转为list转存到user_debt表中
        List<GzxUserDebt> userList = JSON.parseArray(allUser, GzxUserDebt.class);
        log.info("同步在催人力所属项目所属小组 {} ", userList.size());
        userDebtMapper.insertUpdateBatch(userList);
    }

    /**
     * 同步在催人力
     */
    @DS("bi")
    private void syncProjectDebt() {
        List<GzxProjectRespDto> list = projectFeign.listCaseDebt();
        log.info("项目在催案件和在催人力：{}", list);
        debtMapper.insertUpdateBatch(list);
    }

    /**
     * 获取项目的在催案件和在催人力
     */
    @Override
    public List<GzxProjectDebtVo> getProjectDebt(List<GzxUserDebtVo> userDebtList) {
        return debtMapper.getProjectDebt(userDebtList);
    }
}
